/**************************************************************************************
 *                    .::::.
 *                  .::::::::.
 *                 ::::::::::: 
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::  在人间，有谁活着不像是一场炼狱 
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 * ----------------- Loading 摩比秀 
 * ----------------  yaohuitao@100tal.com
 * ----------------- 20180905
 * ヽ｀、ヽ｀｀、ヽ｀ヽ｀、、ヽ ｀ヽ 、ヽ｀🌙｀ヽヽ｀ヽ、ヽ｀ヽ｀、ヽ｀｀、ヽ 
 * 、｀｀、 ｀、ヽ｀ 、、ヽ｀｀、ヽ、｀｀、、ヽ｀｀、 、ヽヽ｀、｀、、ヽヽ、｀｀
 * 、 、 ヽ｀、ヽ｀｀、ヽ｀ヽ｀、、ヽ ｀ヽ 、ヽ｀｀ヽ、💃｀ヽ🏃、、🚶｀🚶🚶ヽ｀、
***************************************************************************************/

class LoadingUI extends egret.Sprite implements RES.PromiseTaskReporter {
    private tipsArr=[
        "购买服装后，记得回到我的家穿上哦~",
        "清理宠物便便，可以增加宠物的开心值哦！",
        '幸运抽奖每天可以免费抽一次哦~',
        '每周签到可以获取神秘礼品哦！',
        '宠物挨饿会影响身体发育的，赶紧做题挣摩比星买食物吧'
    ];

    public constructor(tarr?:Array<string>) {
        super();
        if(tarr){
            this.tipsArr = tarr;
        }
        this.createView();
    }
    

    private textLoading:egret.TextField; //加载进度值
    private textTips: egret.TextField;   //提示

    private createView(): void {
        this.textLoading = new egret.TextField();
        this.textTips = new egret.TextField();
        this.addChild(this.textLoading);
        this.addChild(this.textTips);
        this.startAnimation();
    }

    public onProgress(current: number, total: number): void {
        this.textLoading.text = `Loading...${current}/${total}`;
    }

    //加载成功后执行销毁
    public removeLoading(){
        egret.Tween.removeAllTweens();
        if(this.parent){
            this.parent.removeChild(this);
        }
    }

    /**
     * 字体动画
     */
    private startAnimation() {
        let parser = new egret.HtmlTextParser();
        let tips = Utils.shuffle(this.tipsArr);
        let textflowArr = tips.map(text => parser.parse("<font color=0x00ff0c>"+text+"</font>"));
        let count = -1;
        let change = () => {
            count++;
            if (count >= textflowArr.length) {
                count = 0;
            }
            let textFlow = textflowArr[count];
            // 切换描述内容
            this.textTips.textFlow = textFlow;
            let tw = egret.Tween.get(this.textTips);
            tw.to({ "alpha": 1 }, 200);
            tw.wait(2000);
            tw.to({ "alpha": 0 }, 200);
            tw.call(change, this);
        };
        change();
    }
}
